package com.ws.q1.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.q1.bean.GongYingShang;
import com.ws.q1.bean.LanMu;
import com.ws.q1.bean.XianLu;
import com.ws.q1.conifg.Res;
import com.ws.q1.service.GongYingShangService;
import com.ws.q1.service.LanMuService;
import com.ws.q1.service.XianLuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/xianlu")
public class XianLuController {
    @Autowired
    private XianLuService xianLuService;
    @Autowired
    private GongYingShangService gongYingShangService;
    @Autowired
    private LanMuService lanMuService;

    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) String chufadi,
                       @RequestParam(required = false) String mudidi,
                       @RequestParam(required = false) Double price,
                       @RequestParam(required = false) Integer gongyingshangId,
                       @RequestParam(defaultValue = "1") Integer currentPage) throws SQLException {
        // 将每页显示的记录数改为 3
        PageHelper.startPage(currentPage, 3);
        List<XianLu> list = xianLuService.pageList(name, chufadi, mudidi, price, gongyingshangId);
        PageInfo<XianLu> pageInfo = new PageInfo<>(list);

        // 计算分页范围
        int totalPages = pageInfo.getPages();
        int startPage = Math.max(1, currentPage - 4);
        int endPage = Math.min(totalPages, currentPage + 4);

        // 使用 HashMap 来处理可能为 null 的参数
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("chufadi", chufadi);
        params.put("mudidi", mudidi);
        params.put("price", price);
        params.put("gongyingshangId", gongyingshangId);

        model.addAttribute("list", pageInfo.getList());
        System.out.println("数据: " + pageInfo.getList());
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("startPage", startPage);
        model.addAttribute("endPage", endPage);
        model.addAttribute("params", params);

        cha(model);
        return "xianlu/list";
    }

    @RequestMapping("/cha")
    public String cha(Model model) {
        // 添加异常处理
        try {
            List<LanMu> list1 = lanMuService.list1();
            List<GongYingShang> list2 = gongYingShangService.list2();
            model.addAttribute("list1", list1);
            model.addAttribute("list2", list2);
        } catch (Exception e) {
            System.err.println("数据加载失败: " + e.getMessage());
            model.addAttribute("error", "数据加载失败");
        }
        return "xianlu/add";
    }

    @RequestMapping("/tiao")
    public String tiao(Model model) {
        // 调用 cha 方法加载栏目和供应商数据
        cha(model);
        return "xianlu/add";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        xianLuService.deleteById(id);
        return "redirect:/xianlu/list";
    }

    @RequestMapping("/add")
    public String zengjia(XianLu xianLu, MultipartFile image, Model model) throws IOException {
        System.out.println(xianLu.getTou());

        // 验证线路名称是否为空
        if (xianLu.getName() == null || xianLu.getName().isEmpty()) {
            model.addAttribute("error", "线路名称不能为空");
            cha(model);
            return "xianlu/add";
        }
        // 处理图片上传
        if (image != null && image.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + image.getOriginalFilename().split("\\.")[1];
            xianLu.setTou(fileName);
            image.transferTo(new File(Res.fileDir + fileName));
        } else {
            xianLu.setTou("");
        }
        // 添加旅游线路
        int add = xianLuService.add(xianLu);
        if (add > 0) {
            // 添加成功，重定向到列表页面
            return "redirect:/xianlu/list";
        } else {
            // 添加失败，返回错误信息并重新渲染添加页面
            model.addAttribute("error", "添加旅游线路失败");
            cha(model);
            return "xianlu/add";
        }
    }

    @RequestMapping("/update")
    public String update(XianLu xianlu, MultipartFile image, Model model) throws IOException {
        // 添加日志
        System.out.println("Received XianLu: " + xianlu);
        System.out.println("Received MultipartFile: " + image);
        cha(model);
        if (image != null && !image.isEmpty() && image.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + image.getOriginalFilename().split("\\.")[1];
            xianlu.setTou(fileName);
            image.transferTo(new File(Res.fileDir + fileName));
        } else {
            xianlu.setTou("");
        }
        int update = xianLuService.update(xianlu);
        if (update > 0) {
            return "redirect:/xianlu/list";
        } else {
            model.addAttribute("error", "更新旅游线路失败");
            cha(model);
            return "xianlu/update";
        }
    }


    @RequestMapping("/huixian")
    public String huixian(Integer id, Model model) {
        XianLu huixian = xianLuService.huixian(id);
        model.addAttribute("huixian", huixian);
        try {
            List<LanMu> list1 = lanMuService.list1();
            List<GongYingShang> list2 = gongYingShangService.list2();
            model.addAttribute("list1", list1);
            model.addAttribute("list2", list2);
        } catch (Exception e) {
            System.err.println("数据加载失败: " + e.getMessage());
            model.addAttribute("error", "数据加载失败");
        }

        return "xianlu/update";
    }


    //是否轮播方法
    @RequestMapping("/lunbo")
    public String lunbo(Integer id, Model model) {
        XianLu huixian = xianLuService.huixian(id);
        if (huixian.getLunbo() == 0) {
            huixian.setLunbo(1);
        } else {
            huixian.setLunbo(0);
        }
        xianLuService.update(huixian);
        return "redirect:/xianlu/list";
    }

    //让他轮播
    @RequestMapping("/lunbo1")
    public String lunbo1(Integer id, Model model) {
        XianLu huixian = xianLuService.huixian(id);
        huixian.setLunbo(1);
        xianLuService.update(huixian);
        return "redirect:/xianlu/list";
    }








}












